package com.iiapk.gridnew.fragment;

import java.lang.reflect.Field;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.iiapk.gridnew.FlingGalleryActivity;
import com.iiapk.gridnew.R;
import com.iiapk.gridnew.domain.ColumnType;

public class GridFragment extends Fragment {
	
	private GridView main_gridNews;
	private Animation shake;
	private List<ColumnType> list;
	private Context mContext;
	
	public GridFragment(List<ColumnType> list) {
		this.list=list;
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		mContext = inflater.getContext();
		View view1 = inflater.inflate(R.layout.main_grid, container, false); 
		main_gridNews = (GridView) view1.findViewById(R.id.main_gridNews);
		main_gridNews.setAdapter(new ImageAdapter(inflater.getContext()));
		main_gridNews.setSelector(new ColorDrawable(Color.TRANSPARENT));
		shake = AnimationUtils.loadAnimation(inflater.getContext(), R.anim.shake);
		shake.setFillAfter(false);
		main_gridNews.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
				view.startAnimation(shake);
				Intent intent = new Intent(mContext,FlingGalleryActivity.class);
				intent.putExtra("id", list.get(position).id);
				intent.putExtra("title", list.get(position).title);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.push_left_in,R.anim.hold);
			}
		});
		main_gridNews.setOnItemLongClickListener(new OnItemLongClickListener(){
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,int position, long id) {
				return true;
			}
		});
        return view1;
	}
	
	static class ViewHolder{
		ImageView src;
		ImageView bg;
		TextView text;
		String _id;
	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;
		Vibrator vibrator;   
		ViewHolder viewHolder;
		public ImageAdapter(Context c) {
			mContext = c;
		}
		public int getCount() {
			return list.size();
		}
		public Object getItem(int position) {
			return position;
		}
		public long getItemId(int position) {
			return position;
		}
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = View.inflate(mContext, R.layout.main_grid_item, null);
				viewHolder.src = (ImageView)convertView.findViewById(R.id.main_item_icon);
				viewHolder.bg = (ImageView)convertView.findViewById(R.id.main_item_icon_bg);
				viewHolder.text = (TextView)convertView.findViewById(R.id.main_item_text);
				convertView.setTag(viewHolder);
			} 
			viewHolder = (ViewHolder)convertView.getTag();
			viewHolder._id=list.get(position).id;
			try {
				Field field = R.drawable.class.getField(list.get(position).icon);
				viewHolder.src.setImageResource((Integer)field.get(field.getName()));
			}catch (Exception e) {
				viewHolder.src.setImageResource(R.drawable.ic_launcher);
			}
			viewHolder.text.setText(list.get(position).title);
			viewHolder.src.setAlpha(200);
			return convertView;
		}
	}
}